package com.emar.adx.yiqifa.iostream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;

import java.io.File;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

/**
 * Created by xiaoliang on 2016/10/23.
 */
public class FileReaders {


    public void read() throws Exception {
        InputStream in = new URL("http://commons.apache.org").openStream();
        try {
            System.out.println(IOUtils.toString(in,"utf-8"));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(in);
        }
    }

    public void readFile() throws Exception {
        File file = new File("D:\\export\\logs\\data-monitor\\logs\\logs/data-monitor.log.2016-09-04.log");

        LineIterator it = FileUtils.lineIterator(file, "UTF-8");

        try {
            while (it.hasNext()) {
                String line = it.nextLine();
                System.out.println(line);
                /// do something with line
            }
        } finally {
            LineIterator.closeQuietly(it);
        }
    }

}
